package com.hyfrogx.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyfrogx.common.utils.MD5Utils;
import com.hyfrogx.modules.app.dao.HyAskCodeDao;
import com.hyfrogx.modules.app.entity.HyAskCode;
import com.hyfrogx.modules.app.entity.User;
import com.hyfrogx.modules.app.service.HyAskCodeService;
import com.hyfrogx.modules.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @version 1.0.0
 * @Author: lfh
 * @Date: 2019/10/23 9:24
 * @Company 汉咏科技
 */
@Service
public class HyAskCodeServiceImpl extends ServiceImpl<HyAskCodeDao, HyAskCode> implements HyAskCodeService {

    @Autowired
    UserService userService;

    @Override
    public String createAskCode(Long userId){
        HyAskCode hyAskCode = this.getById(userId);
        if(hyAskCode != null){
            return hyAskCode.getAskcode();
        }
        User user = userService.getById(userId);
        HyAskCode askCode = new HyAskCode();
        askCode.setUid(userId);
        askCode.setAskcode(MD5Utils.MD5(user.getPhone()).substring(0, 8));
        boolean result = this.save(askCode);
        if(result){
            return askCode.getAskcode();
        }
        return "获取邀请码失败";
    }


    @Override
    public HyAskCode getAskCodeByCode(String code){
        // TODO Auto-generated method stub
        HyAskCode askCode = this.getOne(new QueryWrapper<HyAskCode>().eq("askcode",code));
        return askCode;
    }
}
